package com.qujoy.cloud.book.controller;

import com.qujoy.cloud.book.model.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("book")
public class BookController {

    private Logger logger = LoggerFactory.getLogger(BookController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("getByID/{bookID}")
    public Book getByID(@PathVariable("bookID") Long bookID) {
        logger.error("host:{}", discoveryClient.getLocalServiceInstance().getHost());
        logger.error("serviceId:{}", discoveryClient.getLocalServiceInstance().getServiceId());
        return new Book(bookID, "spring cloud ___" + bookID);
    }

    @RequestMapping("serviceInstances/{serviceId}")
    public List<ServiceInstance> getByID(@PathVariable("serviceId") String serviceId) {
        return discoveryClient.getInstances(serviceId);
    }
}
